package cn.com.generaldata.os.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.nutz.json.Json;
import org.nutz.json.JsonFormat;
import org.nutz.lang.Streams;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import cn.com.generaldata.os.service.impl.GisShowDataService;
import cn.com.generaldata.os.vo.ReportIndicatorQueryVO;

/**
 * 指标汇总Controller.
 */
public class GisServerController extends MultiActionController {

	private GisShowDataService gisService;


	public void setGisService(GisShowDataService gisService) {
		this.gisService = gisService;
	}

	/**
	 * 查询企业图片
	 */
	public void queryUnitPics(HttpServletRequest request, HttpServletResponse response,ReportIndicatorQueryVO queryVO) throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		// TODO Auto-generated method stub
		List<String> queryReportData = gisService
				.queryUnitPics(queryVO.getUnitName());
		String jsonp = request.getParameter("callback");
		response.getWriter().print(jsonp + "(" + Json.toJson(queryReportData, JsonFormat.compact()) + ")");
	}

	/**
	 * 访问图片
	 * @param request
	 * @param response
	 * @param queryVO
	 * @throws Exception
	 */
	public void getPic(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String picId = request.getParameter("id");
		String pic = gisService.getPic(picId);

		OutputStream out = null;

		try {
			response.setContentType("application/octet-stream");
			response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", "pic"));

			InputStream fis = new BufferedInputStream(new FileInputStream(pic));
			byte[] buffer = new byte[fis.available()];
			fis.read(buffer);
			fis.close();
			response.reset();
			out = response.getOutputStream();

			out.write(buffer);

			out.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

			Streams.safeClose(out);
		}
	}
	
	/**
	 * 下载数据文件
	 * @param request
	 * @param response
	 * @param queryVO
	 * @throws Exception
	 */
	public void generalExcel(HttpServletRequest request, HttpServletResponse response, ReportIndicatorQueryVO queryVO)
			throws Exception {
		String excel = gisService.generalTableExcel(queryVO);
		if (!StringUtils.isBlank(excel)) {
			File file = new File(excel); // 路径是存文件的路径
			// 根据该路径创建文件对象
			InputStream in = new FileInputStream(file); // 创建文件字节输入流
			OutputStream os = response.getOutputStream(); // 创建输出流对象
			response.addHeader("Content-Disposition", "attachment;filename="
					+ new String(file.getName().getBytes("GBK"), "ISO-8859-1")); // 设置应答头信息
			response.addHeader("Content-Length", file.length() + "");
			response.setCharacterEncoding("GBK");
			response.setContentType("application/octet-stream");
			int data = 0;
			while ((data = in.read()) != -1) { // 循环读取文件
				os.write(data); // 向指定目录中写文件
			}
			os.close(); // 关闭流
			in.close();
		}
	}

	
}
